package com.tdx.blog_nowcoder.intercepter;

import com.tdx.blog_nowcoder.entity.User;
import com.tdx.blog_nowcoder.service.MessageService;
import com.tdx.blog_nowcoder.utils.ThreadHostHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* @description: TODO
* @author 何可人
* @date 2022/7/7 11:33
* @version 1.0
*/

//处理首页消息的数量
@Component
public class MessageNoticeIntercepter implements HandlerInterceptor {

    @Autowired
    private ThreadHostHolder hostHolder;

    @Autowired
    private MessageService messageService;

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response,
                           Object handler, ModelAndView modelAndView) throws Exception {

        User user = hostHolder.getUser();
        if(user!=null&&modelAndView!=null){
            //私信未读
            int letterUnReadCount = messageService.findMessageUnReadCount(user.getId(),null);
            //系统通知未读数量
            int noticeUnReadCount = messageService.findNoticesUnreadCount(user.getId(),null);

            modelAndView.addObject("allUnreadCount",
                    letterUnReadCount + noticeUnReadCount);
        }
    }
}
